// #include <cstdio>
// #include <cstdlib>
// #include <iostream>

// int main()
// {
// 	int a = 1;
// 	int* p = (int*)malloc(10*sizeof(int));
// 	char* pp = (char*)&a;
// 	if(*pp == 1){
// 		std::cout << "小端" << std::endl;
// 	}
// 	std::cout << &a << std::endl;
// 	std::cout << p << std::endl;
// 	return 0;
// }

//"172.27.14.85"  点分式十进制字符串风格的ip地址

#include "udpserver.hpp"

// std::string handler(const std::string& str)
// {
// 	std::string ret = "server get a msg : ";
// 	ret += str;
// 	std::cout << ret << std::endl;
// 	return ret;
// }

std::string handler(const std::string& str,const std::string& clientip,uint16_t clientport)
{
	std::string ret = clientip + ":" + std::to_string(clientport) + " send server a msg : ";
	ret += str;
	std::cout << ret << std::endl;
	return ret;
}

bool safecheck(const std::string& cmd)
{

	std::vector<std::string> warn = {
		"rm",
		"while",
		"kill",
		"mv",
		"cp",
		"top",
		"sudo",
		"unlink"
	};
	for(auto& s : warn){
		if(cmd.find(s) != std::string::npos) return false;
	}
	return true;
}

std::string ExcuteCommand(const std::string& cmd)
{
	std::cout << "get a request cmd : " << cmd << std::endl;
	if(!safecheck(cmd))
	{
		perror("error");
		return "man error";
	}
	FILE* fp = popen(cmd.c_str(),"r");
	if(fp == nullptr)
	{
		std::cout << "popen error" << std::endl;
		return "error";
	}
	char buffer[size];
	std::string ret;
	while(true)
	{
		buffer[0] = 0;
		char* ok = fgets(buffer,sizeof(buffer),fp);
		if(ok == NULL){
			break;
		}
		ret += buffer;
	}
	fclose(fp);
	// std::cout << ret << std::endl;
	return ret;

}

void Usage(const std::string& s)
{
	std::cout << "\n\t" << s << " port[1024+]" << std::endl;
}

int main(int argc,char* argv[])
{
	if(argc != 2){
		Usage(argv[0]);
		return 0;
	}
	uint16_t port = std::stoi(argv[1]);
	std::unique_ptr<udpserver> up(new udpserver(port));
	up->Init();
	// up->Run(ExcuteCommand);
	up->Run();
	return 0;
}
